package main

import (
	"errors"
	"fmt"
	"study5/jysp/crawl"
	"study5/jysp/parse"
	"study5/jysp/save_data"
	"sync"
)

var listUrl = "http://search.jiayuan.com/v2/search_v2.php"

//const SAVE_TYPE = "mysql"
const SAVE_TYPE = "text"

var wg = sync.WaitGroup{}

func main() {
	start,end := getPageInfo()

	//fmt.Println(start,end)
	if start  > end{
		panic(errors.New("the end number must lee than the start number"))
	}

	startPlay(start,end)

}


func startPlay(start,end int){

	wg.Add(end - start + 1)

	for ; start <= end;start++ {
		go doParse(start)
		//fmt.Println(start)
	}
	wg.Wait()
	fmt.Println("bingo,parse success")
}


func doParse(index int){

	defer wg.Done()
	fmt.Printf("start parse the page of %d ......\n",index)

	ch := make(chan []byte)

	crawler := crawl.NewCrawl(listUrl,index,ch)	//爬取器
	parser := parse.NewParser(ch)	//解析器
	var saver save_data.Saver

	go crawler.StartGetData()	//开始爬取
	data,err := parser.StartParse()	//解析
	if err != nil{
		fmt.Printf("parse the page of %d occur err:%s\n",index,err)
		return
	}



	if SAVE_TYPE == "text"{
		saver = save_data.NewTextSaver(fmt.Sprintf("page_of_%d",index))
	}else if SAVE_TYPE == "mysql"{
		saver = save_data.NewMysqlSaver()
	}

	_,err = saver.Save(data)
	if err != nil{
		fmt.Printf("save the page of %d occur error:%s\n",index,err)
		return
	}
	fmt.Printf("parse the page of %d success !!!!\n",index)

}



func getPageInfo()(int,int){
	fmt.Println("please input the page of start")
	var start,end int
	fmt.Scan(&start)
	fmt.Println("please input the page of end")
	fmt.Scan(&end)
	return start,end
}
